package com.business.sys.controller;


import com.business.common.dto.R;
import com.business.redis.ExpiresTimeEnum;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisKeysUtil;
import com.business.redis.utils.RedisUtil;
import com.business.sys.entity.Area;
import com.business.sys.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 区域表: 后端controller类
 *
 * @author chenjiehong
 * @date 2019-04-28 15:03:17
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/area")
public class AreaController {


    @Autowired
    private AreaService sysAreaServiceImpl;

    /**
     * 查询区域表
     *
     * @param parentId
     * @date 2019-04-28 15:03:17
     */
    @GetMapping("/query")
    @ResponseBody
    public R query(@RequestParam(required = false) Integer parentId) {
        String key = String.format(RedisKeyConstant.AREA_QUERY, parentId);
        if (!RedisUtil.hasKey(key)) {
            List<Area> r = sysAreaServiceImpl.query(parentId);
            RedisUtil.setValue(key, r, ExpiresTimeEnum.LEVEL_12.getExpiresTime());
            return R.success(r);
        } else {
            return R.success(RedisUtil.getArray(key, Area.class));
        }
    }


    /**
     * 查询所有城市
     *
     * @date 2019-04-28 15:03:17
     */
    @GetMapping("/queryAllCity")
    @ResponseBody
    public R query() {
        String key = RedisKeyConstant.AREA_QUERY_ALL_CITY;
        if (!RedisUtil.hasKey(key)) {
            List<Area> r = sysAreaServiceImpl.queryAllCity();
            RedisUtil.setValue(key, r, ExpiresTimeEnum.LEVEL_12.getExpiresTime());
            return R.success(r);
        } else {
            return R.success(RedisUtil.getArray(key, Area.class));
        }
    }

    /**
     * 查询所有区域
     *
     * @date 2019-04-28 15:03:17
     */
    @GetMapping("/queryAll")
    @ResponseBody
    public R queryAll() {
        String key = RedisKeyConstant.AREA_QUERY_ALL;
        if (!RedisUtil.hasKey(key)) {
            List<Map<String, Object>> r = sysAreaServiceImpl.queryAll();
            RedisUtil.setValue(key, r);
            return R.success(r);
        } else {
            return R.success(RedisUtil.getArray(key, Area.class));
        }
    }
}